<?php
class UsersController extends AppController {

	var $name = 'Users';

	function beforeFilter(){
		parent::beforeFilter();
		$allowed_actions=array('view','add','checklogin', 'login', 'logout');
		if (!in_array($this->action,$allowed_actions)){
			$this->redirect(array('controller' => 'songs', 'action' => 'index'));
		}
		$this->Auth->allow('add', 'checklogin', 'view');

		if ($this->action == 'add' || $this->action == 'edit') {
			$this->Auth->authenticate = $this->User;
		}

	}

	function login() {
		App::Import('Model', 'Group'); 
		$this->Group = new Group; 
		App::Import('Model', 'Category'); 
		$this->Category = new Category; 
		$groups = $this->Group->find('all', array('order'=>array('Group.title asc')));
		$categories = $this->Category->find('all',array('order'=>array('Category.title asc')));
		$this->set(compact('categories', 'groups'));
	}

	function logout() {
		$this->redirect($this->Auth->logout());
	}
	function index() {
		$this->User->recursive = 0;
		$this->set('users', $this->paginate());
	}

	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid user', true));
			$this->redirect(array('action' => 'index'));
		}
		$user= $this->User->read(null, $id);

		//for rss
		$channel = array('title' => 'AUDIVIT | List of '.$user['User']['username'],
			'link' => '/users/view/'.$user['User']['id'].'/rss',
			'description' => 'Recent song posts of '.$user['User']['username'],
			'language' => 'en-us',
		);

		//loading scores for user
		$this->loadModel('Score');
		$conditions = array("Score.user_id" => $this->Auth->user('id'));
		$scores = $this->Score->find('all', array('conditions' => $conditions));

		$conditions = array("Song.user_id" => $id);
		App::Import('Model', 'Song'); 
		$this->Song = new Song; 
		$this->paginate = array('order' => 'Song.score/(now()- Song.date) DESC', 'limit' => '20','conditions'=> $conditions);
		$songs = $this->paginate('Song');
		foreach ($songs as $i=>$song){
			$songs[$i]['Sitemask']['embedurl'] = preg_replace('/__url__/',$song['Song']['url_for_embed'],$song['Sitemask']['embedurl']);
		}

		$this->set( 'songs', $songs); 
		$this->set('scores', $scores);
		App::Import('Model', 'Group'); 
		$this->Group = new Group; 
		$groups = $this->Group->find('all', array('order'=>array('Group.title asc')));
		$categories = $this->Song->Category->find('all',array('order'=>array('Category.title asc')));
		$this->set(compact('user','categories', 'groups','channel'));
	}

	function add() {

		if (!empty($this->data)) {
			if ($this->data['User']['email']==''){
				$this->data['User']['email']= null;
			}
				$this->data['User']['date']= null;
			$this->User->create();
			if ($this->User->save($this->data)) {
				$this->data['User']['password'] = $this->Auth->password($this->data['User']['password']); 
				$this->Auth->login($this->data);
				$this->redirect($this->Auth->redirect());
			}
		} else {
		}
		App::Import('Model', 'Group'); 
		$this->Group = new Group; 
		App::Import('Model', 'Category'); 
		$this->Category = new Category; 
		$groups = $this->Group->find('all', array('order'=>array('Group.title asc')));
		$categories = $this->Category->find('all',array('order'=>array('Category.title asc')));
		$this->set(compact('categories', 'groups'));
	}

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid user', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->User->save($this->data)) {
				$this->Session->setFlash(__('The user has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The user could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->User->read(null, $id);
		}
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for user', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->User->delete($id)) {
			$this->Session->setFlash(__('User deleted', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('User was not deleted', true));
		$this->redirect(array('action' => 'index'));
	}
	public function checklogin() {          
		$this->autoRender = false; 
		if ($this->Auth->user()) {
			$loggedIn = 1;
		} else {
			$loggedIn = 0;
		}
		return $loggedIn;
	}

}
